package vg.modules.filter.components;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import vg.modules.filter.IFilterPanel;

/**
 * This class realizes action(open in new tab or in current tab).
 * @author tzolotuhin
 */
public class ActionPanel {
	private final IFilterPanel filter;
	private final JPanel view;
	private final JRadioButton openInNewTabRadio, replaceTabRadio, selectRadio;
	/**
	 * Constructor.
	 */
	public ActionPanel(final IFilterPanel filter) {
		this.filter = filter;
		this.view = new JPanel(new GridLayout(3, 1));
		Border titled = BorderFactory.createTitledBorder("Action");
		this.view.setBorder(titled);

		this.selectRadio = new JRadioButton("Select in current tab");
		this.selectRadio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ActionPanel.this.filter.setActionMode(IFilterPanel.SELECT_MODE);
			}
		});
		
		this.openInNewTabRadio = new JRadioButton("Open in new tab");
		this.openInNewTabRadio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ActionPanel.this.filter.setActionMode(IFilterPanel.OPEN_IN_NEW_TAB_MODE);
			}
		});
		
		this.replaceTabRadio = new JRadioButton("Replace tab");
		this.replaceTabRadio.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ActionPanel.this.filter.setActionMode(IFilterPanel.OPEN_IN_CURRENT_TAB_MODE);
			}
		});
		
		ButtonGroup group = new ButtonGroup();
		group.add(this.selectRadio);
		group.add(this.openInNewTabRadio);
		group.add(this.replaceTabRadio);
		this.selectRadio.setSelected(true);
		//package of interface
		this.view.add(this.selectRadio);
		this.view.add(this.openInNewTabRadio);
		this.view.add(this.replaceTabRadio);
	}
	/**
	 * This method updates ui theme.
	 */
	public void updateUITheme() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SwingUtilities.updateComponentTreeUI(ActionPanel.this.view);
			}
		});
	}
	public JPanel getView() {
		return(this.view);
	}
}
